<?php

require __DIR__.'/../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class publish
{
    /** @var  AMQPStreamConnection*/
    public $connect = null;

    private $que = 'hello';

    private $vhost = 'MemberHost';

    public function __construct()
    {
        $this->conn();
    }

    public function conn()
    {
        $host = '192.168.3.203';
        $port = '5672';
        $user = 'MemberAdmin';
        $pwd = '123456';
        $this->connect = new AMQPStreamConnection($host, $port, $user, $pwd,$this->vhost);
    }

    public function send()
    {
        $channel = $this->connect->channel();
        $channel->queue_declare($this->que, false,false,false,false);
        $msg = 'Hello world.'.date('Y-m-d H:i:s');
        $msgObj = new AMQPMessage($msg, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
        $channel->basic_publish($msgObj,'','hello');
        $channel->close();
        $this->connect->close();
        echo '信息发送成功'.PHP_EOL;
    }
}
(new publish())->send();
